{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Bitcoin_Price_Notification",
      "version": "0.3.2",
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "cell_type": "code",
      "metadata": {
        "id": "XbZtO17DDcdd",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "#Description: Get the current price of Bitcoin\n",
        "\n",
        "\n",
        "#Resource: \n",
        "#1. https://realpython.com/python-bitcoin-ifttt/\n",
        "#2. https://www.saltycrane.com/blog/2008/06/how-to-get-current-date-and-time-in/"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "4vd5lz_nIkyb",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "#Import libraries\n",
        "import requests\n",
        "from datetime import datetime"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "le8fGN9LIsDc",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "#Get the URL ticker to get the .json files of the crypto currencies\n",
        "TICKER_API_URL = 'https://api.coinmarketcap.com/v1/ticker/'"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "nXIwzD9IIusr",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "#Function to get the latest crypto currency price of a specific 'crypto' e.g bitcoin, litecoin, etc.\n",
        "# crypto = {bitcoin, litecoin, ethereum, ...}\n",
        "def get_latest_crypto_price( crypto ):\n",
        "    response = requests.get(TICKER_API_URL+crypto)\n",
        "    response_json = response.json()\n",
        "    # Convert the price to a floating point number\n",
        "    return float(response_json[0]['price_usd'])"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "kovZH3-AI5EE",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "649f13f2-8aef-43ad-a55c-233b90045ff0"
      },
      "source": [
        "#Test the function \n",
        "get_latest_crypto_price( 'bitcoin')"
      ],
      "execution_count": 25,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "10013.2162477"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 25
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "tx6ztfohJCic",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "BITCOIN_PRICE_THRESHOLD = 10000  # A threshold set to whatever you like"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "oiypAYgVJHRE",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "def main():\n",
        "  \n",
        "  #Set last_price to -1 to indicate the last price hasn't been recorded yet\n",
        "  last_price = -1\n",
        "  \n",
        "  while True:\n",
        "    \n",
        "    crypto = 'bitcoin'\n",
        "    price = get_latest_crypto_price(crypto)\n",
        "   \n",
        "    #You can use the following variables to get specific datetime\n",
        "    #year = datetime.now().year\n",
        "    #month = datetime.now().month\n",
        "    #day = datetime.now().day\n",
        "    #hour = datetime.now().hour\n",
        "    #minute = datetime.now().minute\n",
        "    #second = datetime.now().second\n",
        "    #microsecond = datetime.now().microsecond\n",
        "    \n",
        "    now = datetime.now()\n",
        "    now.strftime(\"%Y-%m-%d %H:%M\")#Returns format (YYYY-mm-DDTHH:MM:SS.MS)\n",
        "    \n",
        "    #Check if the crypto currency price is less than your threshold\n",
        "    if price < BITCOIN_PRICE_THRESHOLD:\n",
        "      print('The Crypto is lower than your threshold')\n",
        "    \n",
        "    #Print the price of bitcoin only if the current price is different from the last price\n",
        "    if price != last_price:\n",
        "      print(now.isoformat() , ' Bitcoin price = ',price)\n",
        "      last_price = price #update last_price to be the current price\n",
        "    \n",
        "  "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "VguDZp3UJKiM",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 1000
        },
        "outputId": "b62a04d7-a281-4e89-b4ac-15b3ddce2d8c"
      },
      "source": [
        "main()"
      ],
      "execution_count": 35,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "2019-08-24T15:45:02.359099  Bitcoin price =  10014.3385515\n",
            "2019-08-24T15:45:39.031424  Bitcoin price =  10012.9108116\n",
            "2019-08-24T15:48:10.079365  Bitcoin price =  10013.4890172\n",
            "2019-08-24T15:50:41.100118  Bitcoin price =  10003.6795235\n",
            "2019-08-24T15:53:12.071181  Bitcoin price =  10004.4107204\n",
            "2019-08-24T15:55:43.046868  Bitcoin price =  10006.3826106\n",
            "2019-08-24T15:58:14.119517  Bitcoin price =  10005.5588521\n",
            "2019-08-24T16:00:45.067363  Bitcoin price =  10015.4428947\n",
            "2019-08-24T16:03:16.074528  Bitcoin price =  10016.4132309\n",
            "2019-08-24T16:05:47.103513  Bitcoin price =  10013.4346254\n",
            "2019-08-24T16:08:18.036203  Bitcoin price =  10011.7969741\n",
            "2019-08-24T16:10:49.092700  Bitcoin price =  10010.8492184\n",
            "2019-08-24T16:13:20.093886  Bitcoin price =  10004.3835514\n",
            "2019-08-24T16:15:51.035093  Bitcoin price =  10011.3178582\n",
            "2019-08-24T16:18:22.079463  Bitcoin price =  10034.4514398\n",
            "2019-08-24T16:20:53.069944  Bitcoin price =  10050.4855868\n",
            "2019-08-24T16:23:24.105424  Bitcoin price =  10055.1908839\n",
            "2019-08-24T16:25:55.047723  Bitcoin price =  10064.8958507\n",
            "2019-08-24T16:28:26.100019  Bitcoin price =  10061.4209502\n",
            "2019-08-24T16:30:57.077063  Bitcoin price =  10060.9077733\n",
            "2019-08-24T16:33:28.047261  Bitcoin price =  10066.9311984\n",
            "2019-08-24T16:35:59.051840  Bitcoin price =  10060.6809588\n",
            "2019-08-24T16:38:30.266854  Bitcoin price =  10061.5959139\n",
            "2019-08-24T16:41:01.106485  Bitcoin price =  10065.6723407\n",
            "2019-08-24T16:43:32.106853  Bitcoin price =  10067.8732815\n",
            "2019-08-24T16:46:03.112498  Bitcoin price =  10065.0601371\n",
            "2019-08-24T16:48:34.093856  Bitcoin price =  10064.8455309\n",
            "2019-08-24T16:51:05.050032  Bitcoin price =  10071.8474557\n",
            "2019-08-24T16:53:36.034708  Bitcoin price =  10071.1401917\n",
            "2019-08-24T16:56:07.060490  Bitcoin price =  10069.8900848\n",
            "2019-08-24T16:58:38.106224  Bitcoin price =  10071.4094865\n",
            "2019-08-24T17:01:09.071748  Bitcoin price =  10068.5321955\n",
            "2019-08-24T17:03:40.079175  Bitcoin price =  10063.6985553\n",
            "2019-08-24T17:06:11.068876  Bitcoin price =  10059.8240124\n",
            "2019-08-24T17:08:42.096611  Bitcoin price =  10062.5390997\n",
            "2019-08-24T17:11:13.105156  Bitcoin price =  10065.4521643\n",
            "2019-08-24T17:13:44.044891  Bitcoin price =  10059.1166602\n",
            "2019-08-24T17:16:15.112628  Bitcoin price =  10063.3394669\n",
            "2019-08-24T17:18:46.044926  Bitcoin price =  10067.8652667\n",
            "2019-08-24T17:21:17.098111  Bitcoin price =  10062.2251782\n",
            "2019-08-24T17:23:48.083508  Bitcoin price =  10062.9355694\n",
            "2019-08-24T17:26:19.049569  Bitcoin price =  10062.9575217\n",
            "2019-08-24T17:28:50.089290  Bitcoin price =  10062.6244387\n",
            "2019-08-24T17:31:21.059787  Bitcoin price =  10058.1474857\n",
            "2019-08-24T17:33:52.047807  Bitcoin price =  10061.5677194\n",
            "2019-08-24T17:36:23.296680  Bitcoin price =  10062.3045606\n",
            "2019-08-24T17:38:54.033934  Bitcoin price =  10055.3439528\n",
            "2019-08-24T17:41:25.063794  Bitcoin price =  10051.706297\n",
            "2019-08-24T17:43:56.055010  Bitcoin price =  10053.7952326\n",
            "2019-08-24T17:46:27.034298  Bitcoin price =  10056.7857393\n",
            "2019-08-24T17:48:58.040175  Bitcoin price =  10060.5055529\n",
            "2019-08-24T17:51:29.032025  Bitcoin price =  10059.9325236\n",
            "2019-08-24T17:54:00.035298  Bitcoin price =  10057.7839888\n",
            "2019-08-24T17:56:31.076305  Bitcoin price =  10058.3112583\n",
            "2019-08-24T17:59:02.103014  Bitcoin price =  10054.3409408\n",
            "2019-08-24T18:01:33.049623  Bitcoin price =  10055.6019083\n",
            "2019-08-24T18:04:04.119991  Bitcoin price =  10050.902167\n",
            "2019-08-24T18:06:35.054380  Bitcoin price =  10059.8301912\n",
            "2019-08-24T18:09:06.093192  Bitcoin price =  10060.8297625\n",
            "2019-08-24T18:11:37.063439  Bitcoin price =  10062.7837386\n",
            "2019-08-24T18:14:08.033240  Bitcoin price =  10063.2860834\n",
            "2019-08-24T18:16:39.071493  Bitcoin price =  10066.5074489\n",
            "2019-08-24T18:19:10.093989  Bitcoin price =  10065.4238714\n",
            "2019-08-24T18:21:41.078626  Bitcoin price =  10070.8621783\n",
            "2019-08-24T18:24:12.053568  Bitcoin price =  10070.5713925\n",
            "2019-08-24T18:26:43.029006  Bitcoin price =  10072.9086355\n",
            "2019-08-24T18:29:14.037320  Bitcoin price =  10078.1843365\n",
            "2019-08-24T18:31:45.037892  Bitcoin price =  10076.0800699\n",
            "2019-08-24T18:34:16.110668  Bitcoin price =  10077.4386354\n",
            "2019-08-24T18:36:47.073297  Bitcoin price =  10072.9267305\n",
            "2019-08-24T18:39:18.070247  Bitcoin price =  10075.7999553\n",
            "2019-08-24T18:41:49.040236  Bitcoin price =  10076.7557449\n",
            "2019-08-24T18:44:20.079300  Bitcoin price =  10074.332586\n",
            "2019-08-24T18:46:51.028550  Bitcoin price =  10073.3831547\n",
            "2019-08-24T18:49:22.099952  Bitcoin price =  10078.3354158\n",
            "2019-08-24T18:51:53.076257  Bitcoin price =  10079.5011497\n",
            "2019-08-24T18:54:24.053607  Bitcoin price =  10077.126672\n",
            "2019-08-24T18:56:55.138053  Bitcoin price =  10080.9591409\n",
            "2019-08-24T18:59:26.083762  Bitcoin price =  10087.5394553\n",
            "2019-08-24T19:01:57.091925  Bitcoin price =  10083.8718166\n",
            "2019-08-24T19:04:29.409375  Bitcoin price =  10080.2250083\n",
            "2019-08-24T19:06:59.098599  Bitcoin price =  10079.9138893\n",
            "2019-08-24T19:09:30.094092  Bitcoin price =  10078.6763062\n",
            "2019-08-24T19:12:01.044166  Bitcoin price =  10076.209408\n",
            "2019-08-24T19:14:32.128339  Bitcoin price =  10082.395932\n",
            "2019-08-24T19:17:03.092785  Bitcoin price =  10087.3692541\n",
            "2019-08-24T19:19:34.105395  Bitcoin price =  10085.2466597\n",
            "2019-08-24T19:22:05.065136  Bitcoin price =  10089.8194564\n",
            "2019-08-24T19:24:36.090309  Bitcoin price =  10091.4823586\n",
            "2019-08-24T19:27:07.052412  Bitcoin price =  10090.0804149\n",
            "2019-08-24T19:29:38.097504  Bitcoin price =  10090.2197105\n",
            "2019-08-24T19:32:09.105286  Bitcoin price =  10095.4079835\n",
            "2019-08-24T19:34:40.028109  Bitcoin price =  10096.5647863\n",
            "2019-08-24T19:37:11.034229  Bitcoin price =  10098.7247839\n",
            "2019-08-24T19:39:42.038284  Bitcoin price =  10104.6865306\n",
            "2019-08-24T19:42:13.086228  Bitcoin price =  10105.9888742\n",
            "2019-08-24T19:44:44.104595  Bitcoin price =  10102.0171025\n",
            "2019-08-24T19:47:15.029104  Bitcoin price =  10106.650595\n",
            "2019-08-24T19:49:46.059118  Bitcoin price =  10106.8753722\n",
            "2019-08-24T19:52:17.049991  Bitcoin price =  10107.6464613\n",
            "2019-08-24T19:54:48.076870  Bitcoin price =  10106.5707628\n",
            "2019-08-24T19:57:19.055126  Bitcoin price =  10100.2061226\n",
            "2019-08-24T19:59:50.126781  Bitcoin price =  10102.8092474\n",
            "2019-08-24T20:02:21.088456  Bitcoin price =  10109.0704387\n",
            "2019-08-24T20:04:52.112305  Bitcoin price =  10109.6140484\n",
            "2019-08-24T20:07:23.063176  Bitcoin price =  10110.2913217\n",
            "2019-08-24T20:09:54.028786  Bitcoin price =  10112.8417811\n",
            "2019-08-24T20:12:25.037744  Bitcoin price =  10117.9788288\n",
            "2019-08-24T20:14:56.041306  Bitcoin price =  10117.4409699\n",
            "2019-08-24T20:17:27.056244  Bitcoin price =  10120.8357061\n",
            "2019-08-24T20:19:58.040516  Bitcoin price =  10120.3495099\n",
            "2019-08-24T20:22:29.763670  Bitcoin price =  10123.1758796\n",
            "2019-08-24T20:25:01.392129  Bitcoin price =  10127.0953025\n",
            "2019-08-24T20:27:31.040335  Bitcoin price =  10126.2196564\n",
            "2019-08-24T20:30:02.064500  Bitcoin price =  10128.7568883\n",
            "2019-08-24T20:32:33.155510  Bitcoin price =  10138.2458963\n",
            "2019-08-24T20:35:04.050446  Bitcoin price =  10128.9406958\n",
            "2019-08-24T20:37:35.747823  Bitcoin price =  10134.268647\n",
            "2019-08-24T20:40:06.095933  Bitcoin price =  10138.9432536\n",
            "2019-08-24T20:42:37.058029  Bitcoin price =  10133.8028843\n",
            "2019-08-24T20:45:08.046391  Bitcoin price =  10136.8907779\n",
            "2019-08-24T20:47:39.066521  Bitcoin price =  10136.5487419\n",
            "2019-08-24T20:50:10.088419  Bitcoin price =  10129.5703061\n",
            "2019-08-24T20:52:41.056111  Bitcoin price =  10120.7639025\n",
            "2019-08-24T20:55:12.042964  Bitcoin price =  10112.6835054\n",
            "2019-08-24T20:57:43.077362  Bitcoin price =  10109.5058272\n",
            "2019-08-24T21:00:14.091259  Bitcoin price =  10111.9944416\n",
            "2019-08-24T21:02:45.048234  Bitcoin price =  10110.347142\n",
            "2019-08-24T21:05:16.034897  Bitcoin price =  10111.4044629\n",
            "2019-08-24T21:07:47.051875  Bitcoin price =  10103.7976103\n",
            "2019-08-24T21:10:18.038545  Bitcoin price =  10101.8008962\n",
            "2019-08-24T21:12:49.042245  Bitcoin price =  10093.8888621\n",
            "2019-08-24T21:15:20.041288  Bitcoin price =  10090.677529\n",
            "2019-08-24T21:17:51.038096  Bitcoin price =  10089.3793288\n",
            "2019-08-24T21:20:22.066741  Bitcoin price =  10084.8590478\n",
            "2019-08-24T21:22:53.050718  Bitcoin price =  10086.7127705\n",
            "2019-08-24T21:25:24.056833  Bitcoin price =  10081.8406659\n",
            "2019-08-24T21:27:55.719733  Bitcoin price =  10079.4410032\n",
            "2019-08-24T21:30:26.079938  Bitcoin price =  10081.2382242\n",
            "2019-08-24T21:32:57.097178  Bitcoin price =  10084.5154291\n",
            "2019-08-24T21:35:28.035085  Bitcoin price =  10075.0401412\n",
            "2019-08-24T21:37:59.047327  Bitcoin price =  10078.5952386\n",
            "2019-08-24T21:40:30.039951  Bitcoin price =  10075.7811195\n",
            "2019-08-24T21:43:01.088155  Bitcoin price =  10074.1080897\n",
            "2019-08-24T21:45:32.068745  Bitcoin price =  10078.298245\n",
            "2019-08-24T21:48:03.046428  Bitcoin price =  10084.5888242\n",
            "2019-08-24T21:50:34.117302  Bitcoin price =  10087.278091\n"
          ],
          "name": "stdout"
        }
      ]
    }
  ]
}
